<?php
/**
 * ajax删除
 *
 **/
define ( 'IN_templates', "admin" );
include (dirname(__FILE__)."/../includes/init.php");//全局变量

//动作
$act=trim(isset($_POST['act'])?$_POST['act']:"");
//提示消息
$msg="";
//是否执行操作
$msgsuccess="0";//0表示不执行  1表示执行

switch ($act){

	case "ad_img"://删除图片
		
		$id=trim($_POST['id']?$_POST['id']:"");
		$tb_ad_img="`".$tbprefix."ad_img`";
		$imgInfo=$db->get_one("SELECT `id`,`img_src` FROM ".$tb_ad_img." WHERE `id`='".$id."' AND 1");
		$filenames=isset($imgInfo['img_src'])?($imgInfo['img_src']):"";
		
		if(file_exists($filenames)){@unlink("../".$filenames);}
		$dataArray['img_src']="";
		$update_img=$db->update($tb_ad_img,$dataArray,$condition="`id`='".$id."' AND 1");
		if($update_img==true){
			$msg='{"root":[{"status":"true","Description":"删除图片成功"}]}';
		}else{
			$msg='{"root":[{"status":"false","Description":"删除图片失败"}]}';
		}
		break;

	case "admin"://删除管理员

		//登录的管理员id
		$input_adminid=trim(isset($_SESSION['user_id'])?$fun->authcode($_SESSION['user_id'], 'DECODE', $pwdconstant,$expiry =$lifeTime):"");
		$admin_user=$db->get_one("SELECT * FROM `".$tbprefix."admin` WHERE `adminid`='{$input_adminid}' AND `state`='1' AND `is_valid`='1' AND 1");
		$adminPower=isset($admin_user['permission'])?$admin_user['permission']:1;
		if($adminPower<1){
			$adminid=trim($_POST['adminid']?$_POST['adminid']:"");//要删除的管理员
			$tb_admin="`".$tbprefix."admin`";
			/*单个删除*/
			if($adminid && $adminid!=$input_adminid){
				$totaladmin=$db->get_one("SELECT COUNT(`adminid`) AS `total` FROM ".$tb_admin." WHERE `permission`='0' AND `adminid`<>'".$adminid."' AND `is_valid`='1' AND 1");
				if($totaladmin['total']<1){
					$msg='{"root":[{"status":"error","Description":"无法删除唯一的系统管理员"}]}';
				}
				else{
					$dataArray['is_valid']="0";
					$del_admin=$db->update($tb_admin,$dataArray,$condition="`adminid`='".$adminid."' AND 1");//删除【隐藏】adminid
					if($del_admin==true)
						$msg='{"root":[{"status":"true","Description":"删除成功"}]}';
					else
						$msg='{"root":[{"status":"false","Description":"删除失败"}]}';
				}

			}else{
				$msg='{"root":[{"status":"false","Description":"无法删除正在使用的管理员"}]}';
			}
		}else{
			$msg='{"root":[{"status":"false","Description":"权限不足"}]}';
		}


		break;
	
	case "area":
			$area_id=$_POST['area_id']?$_POST['area_id']:"";
			$tb_area="`".$tbprefix."area`";
			/*单个删除*/
			if($area_id){
				$total_s=$db->get_one("SELECT COUNT(`infoid`) AS total  FROM ".$tb_area." WHERE  `parentid`='".$area_id."' AND 1");
				if(!empty($total_s['total'])){
					$msg='{"root":[{"status":"have","Description":"无法删除有子类的区域<br/>请先将其子类删除"}]}';
				}else{
					$dataArray['is_valid']="0";
					$del_info_cata=$db->update($tb_area,$dataArray,$condition="`infoid`='".$area_id."' AND 1");			
					if($del_info_cata==true){
					$msg='{"root":[{"status":"true","Description":"删除成功"}]}';
					}
					else{
					$msg='{"root":[{"status":"false","Description":"删除失败"}]}';
					}
				}
			}else{
				$msg='{"root":[{"status":"false","Description":"删除失败"}]}';
			}
		
			break;
	case "":
		$msg="ErrorPage";
		break;

	default:
		$msg='{"root":[{"status":"false","Description":"操作失败"}]}';
		break;
}

echo $msg;
?>